package com.livemeter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;

/*
 * Represents data behind all meters. Used by application to manage all meters and currently used meter.
 * */
public class Meter
{
	private String caption1;
	private String caption2;
	private String title;
	private boolean permanent;
	
	public String getTitle() {return this.title;}
	public String getCaption1() {return this.caption1;}
	public String getCaption2() {return this.caption2;}
	public boolean isPermanent() {return this.permanent;}

	private Meter() //only allow usage via static methods
	{}
	
	private transient boolean m_isActive = false; 
	private transient boolean m_hasBeenShown = false; 

	public boolean isActive() {return m_isActive;}
	public void setActive(boolean active) {m_isActive = active;}
	public boolean hasBeenShown() {return m_hasBeenShown;}
	public void setHasBeenShown() {m_hasBeenShown = true;}
	
	// Static List

	private static ArrayList<Meter> allMeters = new ArrayList<Meter>();
	private static boolean changed; //whether new data has to be stored to filesystem/db
	
	public static void clearAll()
	{
		allMeters.clear();
	}

	public static void removeMeter(Meter meterToRemove)
	{
		final int count = Meter.allMeters.size();
		
		for(int i = 0; i < count; i++)
		{
			if(Meter.allMeters.get(i).title.compareToIgnoreCase(meterToRemove.title) == 0)
			{
				Meter.allMeters.remove(i);
				Meter.changed = true;
				break;
			}
		}
	}
	
	public static Meter getFirstActiveMeter() 
	{
		for(Meter currentMeter : Meter.allMeters)
		{
			if (currentMeter.isActive())
				return currentMeter;
		}
		return null;
	}

	public static void addMeter(Meter m)
	{
		Meter.allMeters.add(m);
		
		if(m.permanent)
			Meter.changed = true;
	}

	public static Meter addNewMeter(String title, String caption1, String caption2, boolean permanent)
	{
		Meter newMeter = new Meter();
		newMeter.title = title;
		newMeter.caption1 = caption1;
		newMeter.caption2 = caption2;
		newMeter.permanent = permanent;
		addMeter(newMeter);
		return newMeter;
	}

	public static Meter getMeter(String meterTitle)
	{
		for(Meter currentMeter : Meter.allMeters)
		{
			if(currentMeter.title.equalsIgnoreCase(meterTitle))
				return currentMeter;
		}

		return null;
	}

	/*
	 * Returns the titles of all available meters.
	 * */
	public static String[] getAllMeterTitles()
	{
		int count = Meter.allMeters.size();

		if(count == 0)
			return new String[0];

		String[] meterTitles = new String[count];

		for(int i = 0; i < count; i++)
			meterTitles[i] = Meter.allMeters.get(i).title;

		return meterTitles;
	}
	
	public static ArrayList<Meter> getActiveMeters()
	{
		ArrayList<Meter> result = new ArrayList<Meter>();
		for (Meter m : Meter.allMeters)
		{
			if (m.isActive())
				result.add(m);
		}
		return result;
	}
	
	  /*
     * Checks whether meter parameters are not empty and meter title is unique.
     * */
	public static boolean checkMeter(String title, String caption1, String caption2)
	{
		if(title.length() == 0 || caption1.length() == 0 || caption2.length() == 0)
			return false;

		//check whether title is unique
		for(Meter currentMeter : Meter.allMeters)
		{
			if(currentMeter.title.compareTo(title) == 0)
				return false;
		}

		return true;
	}

	/*
	 * New meter was added or active meter was changed.
	 * */
	public static boolean isChanged()
	{
		return changed;
	}

	public static void loadMeters(Activity context)
	{
		Meter.allMeters.clear();
		
		SharedPreferences settings = context.getSharedPreferences("Meters", 0);
	    int countMeters = settings.getInt("meterCount", 0);

	    for(int i = 0; i < countMeters; i++)
	    {
	    	String meterEncoded = settings.getString("meter" + i, "");
	    	
	    	if(meterEncoded.length() == 0)
	    		continue;
	    	
	    	String[] meterProps = meterEncoded.split("\\|");
	    	Meter.addNewMeter(meterProps[0], meterProps[1], meterProps[2], true);
	    }   
	    
	    Meter.changed = false;
	}
	
	public static void saveMeters(Activity context)
	{
		if(!Meter.changed)
			return;

		SharedPreferences settings = context.getSharedPreferences("Meters", 0);
		SharedPreferences.Editor editor = settings.edit();
		
		final int count = Meter.allMeters.size();
		
		
		int minusElements = 0;
		int continousIndex = 0;
		
		for(int i = 0; i < count; i++)
		{
			Meter currentMeter = Meter.allMeters.get(i);
			
			if(!currentMeter.permanent)
			{
				minusElements++;
				continue;
			}
			else
			{
				editor.putString("meter" + continousIndex++, currentMeter.title + "|" + currentMeter.caption1 + "|" + currentMeter.caption2);
			}
		}
		
		editor.putInt("meterCount", count - minusElements);
		
		// Commit the edits!
		editor.commit();
		
		Meter.changed = false;
	}
}

